//
//  TopicView.m
//  SimpleTimeDemo
//
//  Created by lanou3g on 15/11/16.
//  Copyright © 2015年 王丽珍. All rights reserved.
//

#import "TopicView.h"
#import "TopicFirstTableViewCell.h"
#import "TopicSecondTableViewCell.h"
#import "MainDataHelper.h"
#import "TopicModel.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"

@interface TopicView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *topicTableView;

@property (nonatomic, strong) UISegmentedControl *topicSegment;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, copy) NSString *string;
@property (nonatomic, copy) NSString *bodyString;

@property (nonatomic, assign) int pageNum;

@end

@implementation TopicView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.topicTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, self.frame.size.width, self.frame.size.height - 94) style:UITableViewStylePlain];
        
        self.topicSegment = [[UISegmentedControl alloc] initWithItems:@[@"Hot", @"New"]];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //刷新次数
    self.pageNum = 1;
    
    self.topicSegment.selectedSegmentIndex = 0;
    self.topicSegment.frame = CGRectMake(100, 0, self.frame.size.width - 2*100, 25);
    [self.topicSegment addTarget:self action:@selector(topicSegment:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.topicSegment];
    
    self.string = @"http://api2.pianke.me/group/posts_hotlist";
    self.bodyString = @"auth=&client=1&deviceid=4B1DC887-3B93-405F-970B-3B2C4C556E2A&limit=10&sort=hot&start=0&version=3.0.6";
    [self loadData];
    
    [self loadTableView];
    
    //刷新操作--第三方
    [self setupRefresh];
}

- (void)setupRefresh {
    self.topicTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pushReload)];
    self.topicTableView.footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(downReload)];
}

- (void)pushReload {
    [self topicSegment:self.topicSegment];
    [self.topicTableView.header beginRefreshing];
}

- (void)downReload {
    self.pageNum++;
    [self topicSegment:self.topicSegment];
    [self.topicTableView.footer beginRefreshing];
}

- (void)topicSegment:(UISegmentedControl *)sender {
    switch (sender.selectedSegmentIndex) {
        case 0:
            self.string = @"http://api2.pianke.me/group/posts_hotlist";
            self.bodyString = [NSString stringWithFormat:@"auth=&client=1&deviceid=4B1DC887-3B93-405F-970B-3B2C4C556E2A&limit=%d&sort=hot&start=0&version=3.0.6", self.pageNum*10];
            [self loadData];
            break;
        case 1:
            self.string = @"http://api2.pianke.me/group/posts_hotlist";
            self.bodyString = [NSString stringWithFormat:@"auth=&client=1&deviceid=4B1DC887-3B93-405F-970B-3B2C4C556E2A&limit=%d&sort=addtime&start=0&version=3.0.6", self.pageNum*10];
            [self loadData];
            break;
            
        default:
            break;
    }
}

- (void)loadData
{
    [MainDataHelper postDataWithUrlString:self.string andBodyString:self.bodyString compare:^(id object) {
        self.dataArray = [NSMutableArray array];
        
        NSDictionary *dic = object;
        NSArray *array = dic[@"data"][@"list"];
        for (NSDictionary *dic1 in array) {
            TopicModel *model = [[TopicModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.dataArray addObject:model];
        }
        
        [self.topicTableView reloadData];
        [self.topicTableView.header endRefreshing];
        [self.topicTableView.footer endRefreshing];
    }];
}

- (void)loadTableView {
    self.topicTableView.delegate = self;
    self.topicTableView.dataSource = self;
    [self addSubview:self.topicTableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TopicModel *topicModel = self.dataArray[indexPath.row];
    
    if ([topicModel.coverimg isEqualToString:@""]) {
        UINib *nib = [UINib nibWithNibName:@"TopicFirstTableViewCell" bundle:nil];
        [self.topicTableView registerNib:nib forCellReuseIdentifier:@"topicFirstCell"];
        TopicFirstTableViewCell *topicCell = [tableView dequeueReusableCellWithIdentifier:@"topicFirstCell" forIndexPath:indexPath];
        topicCell.titleLabel.text = topicModel.title;
        topicCell.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
        topicCell.titleLabel.numberOfLines = 0;
        [topicCell.titleLabel sizeToFit];
        topicCell.contentLabel.text = topicModel.content;
        topicCell.contentLabel.font = [UIFont fontWithName:@"CourierNewPSMT" size:15];
        topicCell.contentLabel.numberOfLines = 0;
        [topicCell.contentLabel sizeToFit];
        topicCell.timeLabel.text = topicModel.addtime_f;
        topicCell.timeLabel.font = [UIFont fontWithName:@"CourierNewPSMT" size:13];
        topicCell.counterLabel.text = [NSString stringWithFormat:@"%@", topicModel.counterList[@"comment"]];
        topicCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return topicCell;
    }else {
        UINib *nib = [UINib nibWithNibName:@"TopicSecondTableViewCell" bundle:nil];
        [self.topicTableView registerNib:nib forCellReuseIdentifier:@"topicSecondCell"];
        TopicSecondTableViewCell *topicCell = [tableView dequeueReusableCellWithIdentifier:@"topicSecondCell" forIndexPath:indexPath];
        topicCell.titleLabel.text = topicModel.title;
        topicCell.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
        topicCell.titleLabel.numberOfLines = 0;
        [topicCell.titleLabel sizeToFit];
        topicCell.contentLabel.text = topicModel.content;
        topicCell.contentLabel.font = [UIFont fontWithName:@"CourierNewPSMT" size:15];
        topicCell.contentLabel.numberOfLines = 0;
        [topicCell.contentLabel sizeToFit];
        topicCell.timeLabel.text = topicModel.addtime_f;
        topicCell.timeLabel.font = [UIFont fontWithName:@"CourierNewPSMT" size:13];
        [topicCell.MusicImageView sd_setImageWithURL:[NSURL URLWithString:topicModel.coverimg]];
        topicCell.counterLabel.text = [NSString stringWithFormat:@"%@", topicModel.counterList[@"comment"]];
        
        topicCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return topicCell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TopicModel *model = self.dataArray[indexPath.row];
    self.contentStr = model.contentid;
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"topic" object:self];
}


@end
